package com.lincoln.framework.bean;

import com.lincoln.framework.AbstractThreadLocalNettyContextBean;
import com.lincoln.framework.interfaces.BaseDeviceInfo;
import com.lincoln.framework.interfaces.BaseProtocol;
import com.lincoln.framework.interfaces.NettyContextBean;

import java.util.List;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 服务器上下文
 *
 * @author lincoln
 * @date 2018/11/30
 */
public class NettyContext {
    public static NettyContextBean nettyContextBean = new AbstractThreadLocalNettyContextBean();
    /**
     * 客户端列表
     */
    private static ConcurrentHashMap<String, BaseDeviceInfo> clients = new ConcurrentHashMap<>();

    /**
     * 添加客户端
     *
     * @param clientId   内部使用的clientId,区分不同客户端用
     * @param deviceInfo
     * @return
     */
    public static boolean addClient(String clientId, BaseDeviceInfo deviceInfo) {
        try {
            clients.put(clientId, deviceInfo);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 通过内部主键clientId获取客户端
     *
     * @param clientId
     * @return
     */
    public static <T extends BaseDeviceInfo> T getClient(String clientId) {
        try {
            return (T) clients.get(clientId);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 通过内部主键clientId踢出客户端
     *
     * @param clientId
     * @return
     */
    public static boolean removeClient(String clientId) {
        try {
            clients.remove(clientId);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 没有登陆的客户端会在一定时间后被销毁,可以通过deviceInfo.login()登陆
     */
    private static List<String> noLoginClients = new Vector<>();

    /**
     * 向未登录列表中加入一个clientId
     * @param clientId
     * @return
     */
    public static boolean addNoLogin(String clientId){
        if(noLoginClients.contains(clientId)){
            return true;
        }
        noLoginClients.add(clientId);
        return true;
    }

    /**
     * 从未登录列表中删除一个clientId
     * @param clientId
     * @return
     */
    public static boolean removeNoLogin(String clientId){
        noLoginClients.remove(clientId);
        return true;
    }

    /**
     * 客户端进入时
     * @param nettyContextBean
     */
    public static void setNettyContextBean(NettyContextBean nettyContextBean) {
        nettyContextBean = nettyContextBean;
    }

    /**
     * 客户端退出时
     */
    public static void remoceNettyContextBean() {
        nettyContextBean.remove();
    }

    /**
     * 获取当前设备
     *
     * @return
     */
    public static <T extends BaseDeviceInfo> T getDevice() {
        return (T)nettyContextBean.getDevice();
    }

    /**
     * 设置当前设备
     *
     * @param deviceInfo
     * @return
     */
    public static boolean setDevice(BaseDeviceInfo deviceInfo) {
        return nettyContextBean.setDevice(deviceInfo);
    }

    /**
     * 获取当前帧
     *
     * @return
     */
    public static BaseProtocol getProtocol() {
        return nettyContextBean.getProtocol();
    }

    /**
     * 设置当前帧
     *
     * @param protocol
     * @return
     */
    public static boolean setProtocol(BaseProtocol protocol) {
        return nettyContextBean.setProtocol(protocol);
    }

}
